package ro.gateway.aida.charts;

import java.util.ArrayList;

import org.jfree.data.DefaultCategoryDataset;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jun 20, 2004
 * Time: 12:15:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class BarChartDataMan {
	public static void populate_category_dataset(DefaultCategoryDataset category_dataset,
										String language,
										String y_nice_name,
										ChartWizzard$ActionForm form) {
		if (form.search_results == null) return;
		ArrayList distinct_rows = ChartUtils.getDistinctValues(form.search_results,
			  1, language,
			  form.x_axe_criteria.startsWith("year#"),
			  form); // serii
		ArrayList distinct_columns = null;
		if (form.x_axe_group_criteria != null) {
			System.out.println("group by:" + form.x_axe_group_criteria);
			distinct_columns = ChartUtils.getDistinctValues(form.search_results,
				  2, language,
				  form.x_axe_group_criteria.startsWith("year#"),
				  form); // grupuri
		}

		for (int i = 0; i < distinct_rows.size(); i++) {
			Comparable row = (Comparable) distinct_rows.get(i);
			Number value = null;
			if (distinct_columns != null) {
				ArrayList items = new ArrayList();
				for (int j = 0; j < distinct_columns.size(); j++) {
					Comparable col = (Comparable) distinct_columns.get(j);

					value = ChartUtils.compute_chart_value(row, col, form);
					if (value.doubleValue() != 0) {
						items.add(new Object[]{
							value, row != null ? row : "--", col != null ? col : "--"
						});
					}
				}
				if (items.size() > 0) {
					for (int j = 0; j < items.size(); j++) {
						Object[] objects = (Object[]) items.get(j);
						category_dataset.addValue((Number) objects[0], (Comparable) objects[1], (Comparable) objects[2]);
					}
				}
			} else {
				value = ChartUtils.compute_chart_value(row, null, form);
				category_dataset.addValue(value, y_nice_name, row != null ? row : "--");
			}
		}
	}

}
